package com.jiyun.service.impl;

import com.jiyun.comfig.R;

import com.jiyun.entity.Technician;
import com.jiyun.entity.User;
import com.jiyun.mapper.UserMapper;


import com.jiyun.service.TechnicianService;
import com.jiyun.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * (User)表服务实现类
 *
 * @author LYT
 * @since 2025-01-17 18:29:06
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        return this.baseMapper.login(user);
    }

    /**
     * 发送验证码
     * @param phone
     * @param code
     * @return
     */
    @Override
    public R sendCode(String phone, String code) {
        Random random = new Random();
        int smsCode =1000 + random.nextInt(9000);
        System.out.println("验证码为："+smsCode);
        redisTemplate.opsForValue().set("code", smsCode);
        return R.ok().put("code", smsCode);
    }

    /**
     * 注册
     * @param user
     */
    @Override
    public void register(User user) {
        user.setUsername("admin");
        user.setIsDelete(0);
        user.setAvatarUrl("https://img1.baidu.com/it/u=534429813,2995452219&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1737219600&t=6e1b33d0b5d130b7f63d6f27cdd65ba4");
        this.baseMapper.register(user);
    }

    /**
     * 检查手机号
     * @param user
     * @return
     */
    @Override
    public User checkPhone(User user) {
        return this.baseMapper.checkPhone(user);
    }

    /**
     * 修改密码
     * @param user
     */
    @Override
    public void updatePassword(User user) {
        this.baseMapper.updatePassword(user);
    }

    /**
     * 查询所有手机号
     * @return
     */
    @Override
    public List<User> findAllPhone() {
        return this.baseMapper.findAllPhone();
    }

    /**
     * 获取优惠券个数
     * @param id
     * @return
     */
    @Override
    public int getCountCoupon(Long id) {
        return userMapper.showCount(id);
    }

    @Override
    public int getFavoriteCount(Long userId) {
        User user = baseMapper.selectById(userId);
        if (user == null || user.getTechnicianIds() == null || user.getTechnicianIds().isEmpty()) {
            return 0;
        }
        return user.getTechnicianIds().split(",").length;
    }

    /**
     * 查询当前账号收藏的技师
     *
     * @param userId 用户ID
     * @return 技师列表
     */
    @Resource
    private TechnicianService technicianService;
    @Override
    public List<Technician> getFavoriteTechnicians(Long userId) {
        User user = baseMapper.selectById(userId);
        if (user == null || user.getTechnicianIds() == null || user.getTechnicianIds().isEmpty()) {
            return List.of();
        }
        List<Long> technicianIds = Arrays.stream(user.getTechnicianIds().split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return technicianService.listByIds(technicianIds);
    }


    @Override
    public Integer updateMonuey(Integer id, Integer money) {
        Integer currentBalance = money;
        if(currentBalance >= 1000 && currentBalance < 3000){
            currentBalance +=100;
        }else if(currentBalance >= 3000 && currentBalance < 5000){
            currentBalance +=400;
        }else if(currentBalance >= 5000){
            currentBalance +=800;
        }
//        System.out.println(currentBalance);
        userMapper.updateMoney(id,currentBalance);
        return currentBalance;
    }

}
